package main.api;

import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import main.GlobalConfig;
import main.ResponseHelper;
import main.TimeHelper;
import main.database.DatabaseMan;
import org.bson.Document;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class LostThingApi {
    @PostMapping("/api/submitlostauthor")
    public ResponseEntity<?> submitLostAuthor(HttpServletRequest request) {
        String name = null;
        Map<String, String[]> map = request.getParameterMap();
        if (map.containsKey("name")) {
            name = map.get("name")[0].trim();
        }

        String result;
        if (name == null || name.isEmpty()) {
            result = ResponseHelper.createBasicJsonObject(404, "没有name这个字段提供给api").toString();
        }
        else {
            MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfLostAuthor);
            FindIterable<Document> findIterable = collection.find(new BasicDBObject("name", name));
            MongoCursor<Document> cursor = findIterable.iterator();

            result = ResponseHelper.createBasicJsonObject(200, "ok").toString();
            if (cursor.hasNext()) {

            }
            else {
                Document document = new Document("name", name).append("createtime", TimeHelper.getFormattedNowString());
                collection.insertOne(document);
            }
            cursor.close();
        }

        return ResponseEntity.ok(result);
    }

    @PostMapping("/api/submitlostbook")
    public ResponseEntity<?> submitLostBook(HttpServletRequest request) {
        String name = null;
        Map<String, String[]> map = request.getParameterMap();
        if (map.containsKey("name")) {
            name = map.get("name")[0].trim();
        }

        String result;
        if (name == null || name.isEmpty()) {
            result = ResponseHelper.createBasicJsonObject(404, "没有name这个字段提供给api").toString();
        }
        else {
            MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfLostBook);
            FindIterable<Document> findIterable = collection.find(new BasicDBObject("name", name));
            MongoCursor<Document> cursor = findIterable.iterator();

            result = ResponseHelper.createBasicJsonObject(200, "ok").toString();
            if (cursor.hasNext()) {

            }
            else {
                Document document = new Document("name", name).append("createtime", TimeHelper.getFormattedNowString());
                collection.insertOne(document);
            }
            cursor.close();
        }

        return ResponseEntity.ok(result);
    }
}
